<?php

declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class BasicCooperaterStatisticsNewTask extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('basic-cooperater-statistics-new-task')// 配置一个参数
            ->addArgument('date_str', Argument::OPTIONAL, '当天时间')
            ->setDescription('查询以前充值的数据(不包含当天)');
    }

    protected function execute(Input $input, Output $output)
    {
        // $dateStr = $input->getArgument('date_str');
        // if ($dateStr) {
        //     $dateTime = date('Ymd', strtotime($dateStr));
        //     $month = date('Ym', strtotime($dateStr));
        // } else {
        //     $dateTime = date('Ymd', time() - 86400);
        //     $month = date('Ym', time() - 86400);
        // }

        // ini_set('memory_limit', '-1');
        // ini_set('max_execution_time', 0);
        //查询以前所有的数据
        //查询所有的剧id
        $videoIdData = Db::name('videos')->where(['is_delete' => 0])->field('id,name')->select()->toArray();
        $videoIds = array_column($videoIdData, 'id');
        //统计数据
        $saveData = [];
        $currentDate = date('Y-m-d', time() - 86400);

      
        $startTime = strtotime(date('Y-m-d', time()) . ' 00:00:00' . " -8 day");
        $endTime = strtotime(date('Y-m-d', time()) . ' 23:59:59' . " -1 day");
        $where = [
            ['status', '=', 1],
            ['pay_time', '>=', $startTime],
            ['pay_time', '<=', $endTime],
            ['video_id', 'in', $videoIds]
        ];
        $payData = Db::name('pay_record')->where($where)->field('video_id, total_fee, consume_date,link_id')->select()->toArray();
        $linkData = Db::name('link')->alias('a')->join('responsibility_person b', 'a.responsibility_person_id=b.id', 'left')->whereIn('company_id', config('enum.self_company_id'))->cache(true, 60)->column('a.id');
        $redata = [];
        foreach($payData as $key => $value) {
            $redata['total_price'][$value['video_id']][$value['consume_date']][] = $value['total_fee'];
            if(in_array($value['link_id'], $linkData)) {
                $redata['self_price'][$value['video_id']][$value['consume_date']][] = $value['total_fee'];
            }
        }

        foreach($videoIdData as $k_E => $v_E) {
              // 获取需要统计的起始日期
        $startDate = date("Y-m-d", time() - (8 * 24 * 60 * 60)); // 统计过去7天的数据，你可以根据需要进行调整
            // 遍历每一天进行统计
            while ($startDate <= $currentDate) {
                // 统计逻辑，根据需要进行具体的数据统计操作
                // 这里只是一个示例，你需要根据实际需求进行相应的统计代码编写

                $dateTime = date('Ymd', strtotime($startDate));
                $month = date('Ym', strtotime($startDate));
                //写入数据
                $rechargeDataAmount = empty($redata['total_price'][$v_E['id']][$dateTime]) ? 0 : array_sum($redata['total_price'][$v_E['id']][$dateTime]);
                $selfDataAmount = empty($redata['self_price'][$v_E['id']][$dateTime]) ? 0 : array_sum($redata['self_price'][$v_E['id']][$dateTime]);
                $saveData = [
                    'date' => $dateTime,
                    'month' => $month,
                    'video_name' => $v_E['name'],
                    'video_id' => $v_E['id'],
                    'total_price' => round($rechargeDataAmount, 2),
                    'self_price' => round($selfDataAmount, 2),
                    'created_at' => time(),
                ];
                $mit = Db::name('daily_cooperater_statistics')->where(['video_id' => $v_E['id'],  'date' => $dateTime])->find();
                if(empty($mit)) {
                    Db::name('daily_cooperater_statistics')->insert($saveData);
                } else {
                    
                    Db::name('daily_cooperater_statistics')->where('id', $mit['id'])->update($saveData);
                }
                // 递增日期
                $startDate = date("Y-m-d", strtotime($startDate . " +1 day"));
            }

            // var_dump($videoEarliestData[$k_E]);exit;
            // }
            
        }
        return true;
    }
}
